import tkinter as tk
from tkinter import filedialog, messagebox
from xmind2excel import xmind2excel

# 创建主窗口
root = tk.Tk()
root.title("Xmind转Excel")

# 定义字符串变量
file_path = tk.StringVar()
text1 = tk.StringVar()
text2 = tk.StringVar()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

# 设置窗口大小
window_width = 410
window_height = 200

# 计算窗口居中位置
x_position = (screen_width / 2) - (window_width / 2)
y_position = (screen_height / 2) - (window_height / 2) - 75

# 设置窗口位置和大小
root.geometry(f"{window_width}x{window_height}+{int(x_position)}+{int(y_position)}")

# 文件选择逻辑
def select_file():
    filetypes = [
        ("Xmind文件", "*.xmind"),
    ]
    try:
        file_path.set(filedialog.askopenfilename(filetypes=filetypes))
    except Exception as e:
        show_error(f"Error selecting file: {e}")

# 显示选择文件按钮选择
file_button = tk.Button(root, text="选择文件", command=select_file)
file_button.place(x=340, y=10, width=60, height=28)

# 显示文件路径的标签和文本框（只读）
file_label = tk.Label(root, text="Xmind文件：")
file_label.place(x=10, y=10, width=75, height=28)
file_entry = tk.Entry(root, textvariable=file_path, state='readonly', width=50)
file_entry.place(x=80, y=10, width=250, height=28)

# 负责人输入框
text1_label = tk.Label(root, text="负责人：")
text1_label.place(x=10, y=45, width=50, height=28)
text1_entry = tk.Entry(root, textvariable=text1, width=50)
text1_entry.place(x=80, y=45, width=250, height=28)

# 目录输入框
text2_label = tk.Label(root, text="目录：")
text2_label.place(x=10, y=82, width=37, height=28)
text2_entry = tk.Entry(root, textvariable=text2, width=50)
text2_entry.place(x=80, y=82, width=250, height=28)

#目录格式提示
readonly_entry = tk.Entry(root, width=15)
readonly_entry.insert(0, r"云效要求多级目录之间用 | 分割，比如：司机小程序|1.0版本|首轮测试")  # 插入初始文本
readonly_entry.config(state='readonly')
readonly_entry.place(x=10, y=118, width=390, height=28)

# 确认按钮
def confirm():
    try:
        txt1 = text1.get()
        txt2 = text2.get()
        file_p = file_path.get()

        # 业务，调用xmind2excel方法
        xmind2excel(xmind_file=file_p, owner=txt1, contents=txt2)
        messagebox.showinfo("确认", "生成的excel文件在源文件同目录下")
    except Exception as e:
        show_error(f"Error during confirmation: {e}")

confirm_button = tk.Button(root, text="开始转换", command=confirm)
confirm_button.place(x=165, y=152, width=80, height=28)

# 显示错误信息的函数
def show_error(error_message):
    messagebox.showerror("Error", error_message)

# 运行主循环
root.mainloop()
